$(function () {
    $("#jqGrid").jqGrid({
        url:  baseURL + 'sys/test/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', width: 45, key: true, hidden:true},
			{ label: '编号', name: 'code', width: 75 },
			{ label: '优惠券名称', name: 'name', width: 75 },
			{ label: '优惠券金额', name: 'money', width: 90 },
			{ label: '有效期', name: 'validity', width: 100 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        	$('a').click(function(){
        		var userDa=$('#jqGrid').jqGrid('getRowData','rowId')
        		console.log(userDa);
        	});
        }
    });
});



var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			name: null
		},
		showList: true,
		title: null,
		coupon: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.coupon = {};
		},
/*		update: function () {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			console.log(1);
			$.get(baseURL + '/sys/sysCustomer/info/'+id,function(r){
				vm.showList = false;
				console.log(r);
                vm.title = "修改";
				vm.customer = r.customer;
				
			});
			 $.ajax({
		        type: "POST",
		        url: "/sys/sysCustomer/info/",
		        contentType: "application/json;charset=utf-8",
		        dataType: "json",
		        data:"id="+id,
		        success: function (r) {
		        	vm.showList = false;
	                vm.title = "修改";
					vm.customer = r.customer;
		        },
		    }); 
		},*/
		update: function () {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			
			vm.showList = false;
            vm.title = "修改";
			
			vm.getUser(id);
		},
		saveOrUpdate: function () {
            if(vm.validator()){
                return ;
            }

			var url = vm.coupon.id != null ? "sys/test/update?validity="+vm.coupon.validity : "sys/sysCustomer/save";
			console.log(baseURL + url);
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.coupon),
			});
			alert('操作成功', function(){
				vm.reload();
			});
		    
		},
		getUser: function(id){
			$.get(baseURL + "sys/test/info?id="+id, function(r){
				vm.coupon = r.coupon;
			});
		},
		del: function () {
			var id = getSelectedRows();
			if(id == null){
				return ;
			}
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/test/delete",
                    contentType: "application/json",
				    data: JSON.stringify(id),
				    success: function(r){
						if(r.code === 0){
							alert('操作成功', function(){
								vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                postData:{'name': vm.q.name
                	},
                page:page 
            }).trigger("reloadGrid");
		},
        validator: function () {
          

        }
	}
});